﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using Middleware;
using Middleware.Naming;
using Middleware.Performance;

namespace Performance.Client
{
    class Program
    {
        static void Main(string[] args) {
            TestMarshallerMessageRequestPerformance();
        }

        private static void TestMarshallerMessageRequestPerformance() {

            #region (Un)MarshallerVoid
            using (StreamWriter fileMarshaller = new StreamWriter("MarshallerVoid.txt", false)) {
                using (StreamWriter fileUnMarshaller = new StreamWriter("UnMarshallerVoid.txt", false)) {

                    MessageRequest request = new MessageRequest();
                    request.MethodName = @"MethodVoid";
                    request.HasReturnValue = false;
                    request.ObjectID = 0;
                    request.Parameters = new object[] { };

                    Stopwatch watch = new Stopwatch();
                    for (int i = 0; i < 10000; i++) {

                        watch.Start();
                        byte[] requestBytes = Marshaller.GetBytes(request);
                        watch.Stop();
                        string outputMarshaller = string.Format("{0}\t{1}\t{2}", i, "MarshallerVoid", watch.Elapsed.ToString());
                        fileMarshaller.WriteLine(outputMarshaller);
                        Console.WriteLine(outputMarshaller);
                        watch.Reset();

                        watch.Start();
                        Marshaller.GetMessage(requestBytes);
                        watch.Stop();
                        string outputUnMarshaller = string.Format("{0}\t{1}\t{2}", i, "UnMarshallerVoid", watch.Elapsed.ToString());
                        fileUnMarshaller.WriteLine(outputUnMarshaller);
                        Console.WriteLine(outputUnMarshaller);
                        watch.Reset();
                    }
                }
            }
            #endregion

            #region (Un)MarshallerInt
            using (StreamWriter fileMarshaller = new StreamWriter("MarshallerInt.txt", false)) {
                using (StreamWriter fileUnMarshaller = new StreamWriter("UnMarshallerInt.txt", false)) {

                    MessageRequest request = new MessageRequest();
                    request.MethodName = @"MethodInt";
                    request.HasReturnValue = false;
                    request.ObjectID = 0;
                    request.Parameters = new object[] { int.MaxValue };

                    Stopwatch watch = new Stopwatch();
                    for (int i = 0; i < 10000; i++) {

                        watch.Start();
                        byte[] requestBytes = Marshaller.GetBytes(request);
                        watch.Stop();
                        string outputMarshaller = string.Format("{0}\t{1}\t{2}", i, "MarshallerInt", watch.Elapsed.ToString());
                        fileMarshaller.WriteLine(outputMarshaller);
                        Console.WriteLine(outputMarshaller);
                        watch.Reset();

                        watch.Start();
                        Marshaller.GetMessage(requestBytes);
                        watch.Stop();
                        string outputUnMarshaller = string.Format("{0}\t{1}\t{2}", i, "UnMarshallerInt", watch.Elapsed.ToString());
                        fileUnMarshaller.WriteLine(outputUnMarshaller);
                        Console.WriteLine(outputUnMarshaller);
                        watch.Reset();
                    }
                }
            }
            #endregion

            #region (Un)MarshallerShort
            using (StreamWriter fileMarshaller = new StreamWriter("MarshallerShort.txt", false)) {
                using (StreamWriter fileUnMarshaller = new StreamWriter("UnMarshallerShort.txt", false)) {

                    MessageRequest request = new MessageRequest();
                    request.MethodName = @"MethodShort";
                    request.HasReturnValue = false;
                    request.ObjectID = 0;
                    request.Parameters = new object[] { short.MaxValue };

                    Stopwatch watch = new Stopwatch();
                    for (int i = 0; i < 10000; i++) {

                        watch.Start();
                        byte[] requestBytes = Marshaller.GetBytes(request);
                        watch.Stop();
                        string outputMarshaller = string.Format("{0}\t{1}\t{2}", i, "MarshallerShort", watch.Elapsed.ToString());
                        fileMarshaller.WriteLine(outputMarshaller);
                        Console.WriteLine(outputMarshaller);
                        watch.Reset();

                        watch.Start();
                        Marshaller.GetMessage(requestBytes);
                        watch.Stop();
                        string outputUnMarshaller = string.Format("{0}\t{1}\t{2}", i, "UnMarshallerShort", watch.Elapsed.ToString());
                        fileUnMarshaller.WriteLine(outputUnMarshaller);
                        Console.WriteLine(outputUnMarshaller);
                        watch.Reset();
                    }
                }
            }
            #endregion

            #region (Un)MarshallerShort
            using (StreamWriter fileMarshaller = new StreamWriter("MarshallerByte.txt", false)) {
                using (StreamWriter fileUnMarshaller = new StreamWriter("UnMarshallerByte.txt", false)) {

                    MessageRequest request = new MessageRequest();
                    request.MethodName = @"MethodByte";
                    request.HasReturnValue = false;
                    request.ObjectID = 0;
                    request.Parameters = new object[] { byte.MaxValue };

                    Stopwatch watch = new Stopwatch();
                    for (int i = 0; i < 10000; i++) {

                        watch.Start();
                        byte[] requestBytes = Marshaller.GetBytes(request);
                        watch.Stop();
                        string outputMarshaller = string.Format("{0}\t{1}\t{2}", i, "MarshallerByte", watch.Elapsed.ToString());
                        fileMarshaller.WriteLine(outputMarshaller);
                        Console.WriteLine(outputMarshaller);
                        watch.Reset();

                        watch.Start();
                        Marshaller.GetMessage(requestBytes);
                        watch.Stop();
                        string outputUnMarshaller = string.Format("{0}\t{1}\t{2}", i, "UnMarshallerByte", watch.Elapsed.ToString());
                        fileUnMarshaller.WriteLine(outputUnMarshaller);
                        Console.WriteLine(outputUnMarshaller);
                        watch.Reset();
                    }
                }
            }
            #endregion

            #region (Un)MarshallerString
            using (StreamWriter fileMarshaller = new StreamWriter("MarshallerString.txt", false)) {
                using (StreamWriter fileUnMarshaller = new StreamWriter("UnMarshallerString.txt", false)) {

                    MessageRequest request = new MessageRequest();
                    request.MethodName = @"MethodString";
                    request.HasReturnValue = false;
                    request.ObjectID = 0;
                    request.Parameters = new object[] { @"09876543210987654321098765432109876543210987654321
                                                          09876543210987654321098765432109876543210987654321
                                                          09876543210987654321098765432109876543210987654321
                                                          09876543210987654321098765432109876543210987654321
                                                          09876543210987654321098765432109876543210987654321
                                                          09876" };

                    Stopwatch watch = new Stopwatch();
                    for (int i = 0; i < 10000; i++) {

                        watch.Start();
                        byte[] requestBytes = Marshaller.GetBytes(request);
                        watch.Stop();
                        string outputMarshaller = string.Format("{0}\t{1}\t{2}", i, "MarshallerString", watch.Elapsed.ToString());
                        fileMarshaller.WriteLine(outputMarshaller);
                        Console.WriteLine(outputMarshaller);
                        watch.Reset();

                        watch.Start();
                        Marshaller.GetMessage(requestBytes);
                        watch.Stop();
                        string outputUnMarshaller = string.Format("{0}\t{1}\t{2}", i, "UnMarshallerString", watch.Elapsed.ToString());
                        fileUnMarshaller.WriteLine(outputUnMarshaller);
                        Console.WriteLine(outputUnMarshaller);
                        watch.Reset();
                    }
                }
            }
            #endregion

            #region (Un)MarshallerIntArray
            using (StreamWriter fileMarshaller = new StreamWriter("MarshallerIntArray.txt", false)) {
                using (StreamWriter fileUnMarshaller = new StreamWriter("UnMarshallerIntArray.txt", false)) {

                    List<int> intList = new List<int>();
                    for (int i = 0; i < 255; i++) { intList.Add(i); }
                    int[] intArray = intList.ToArray();

                    MessageRequest request = new MessageRequest();
                    request.MethodName = @"MethodIntArray";
                    request.HasReturnValue = false;
                    request.ObjectID = 0;
                    request.Parameters = new object[] { intArray };

                    Stopwatch watch = new Stopwatch();
                    for (int i = 0; i < 10000; i++) {

                        watch.Start();
                        byte[] requestBytes = Marshaller.GetBytes(request);
                        watch.Stop();
                        string outputMarshaller = string.Format("{0}\t{1}\t{2}", i, "MarshallerIntArray", watch.Elapsed.ToString());
                        fileMarshaller.WriteLine(outputMarshaller);
                        Console.WriteLine(outputMarshaller);
                        watch.Reset();

                        watch.Start();
                        Marshaller.GetMessage(requestBytes);
                        watch.Stop();
                        string outputUnMarshaller = string.Format("{0}\t{1}\t{2}", i, "UnMarshallerIntArray", watch.Elapsed.ToString());
                        fileUnMarshaller.WriteLine(outputUnMarshaller);
                        Console.WriteLine(outputUnMarshaller);
                        watch.Reset();
                    }
                }
            }
            #endregion
        }

        private void WriteFileMiddlewareRequestTime() {

            NamingStub naming = new NamingStub();
            IPerformance performance = naming.Lookup("Performance") as IPerformance;

            Stopwatch watch = new Stopwatch();

            using (StreamWriter file = new StreamWriter("MethodVoid.txt", false)) {
                for (int i = 0; i < 10000; i++) {
                    watch.Start();
                    performance.MethodVoid();
                    watch.Stop();
                    string output = string.Format("{0}\t{1}\t{2}", i, "MethodVoid", watch.Elapsed.ToString());
                    file.WriteLine(output);
                    Console.WriteLine(output);
                    watch.Reset();
                }
            }

            using (StreamWriter file = new StreamWriter("MethodByte.txt", false)) {
                for (int i = 0; i < 10000; i++) {
                    watch.Start();
                    performance.MethodByte(byte.MaxValue);
                    watch.Stop();
                    string output = string.Format("{0}\t{1}\t{2}", i, "MethodByte", watch.Elapsed.ToString());
                    file.WriteLine(output);
                    Console.WriteLine(output);
                    watch.Reset();
                }
            }

            using (StreamWriter file = new StreamWriter("MethodInt.txt", false)) {
                for (int i = 0; i < 10000; i++) {
                    watch.Start();
                    performance.MethodInt(int.MaxValue);
                    watch.Stop();
                    string output = string.Format("{0}\t{1}\t{2}", i, "MethodInt", watch.Elapsed.ToString());
                    file.WriteLine(output);
                    Console.WriteLine(output);
                    watch.Reset();
                }
            }

            using (StreamWriter file = new StreamWriter("MethodString.txt", false)) {
                for (int i = 0; i < 10000; i++) {
                    watch.Start();
                    performance.MethodString(@"09876543210987654321098765432109876543210987654321
                                           09876543210987654321098765432109876543210987654321
                                           09876543210987654321098765432109876543210987654321
                                           09876543210987654321098765432109876543210987654321
                                           09876543210987654321098765432109876543210987654321
                                           09876");
                    watch.Stop();
                    string output = string.Format("{0}\t{1}\t{2}", i, "MethodString", watch.Elapsed.ToString());
                    file.WriteLine(output);
                    Console.WriteLine(output);
                    watch.Reset();
                }
            }

            List<int> intList = new List<int>();
            for (int i = 0; i < 255; i++) { intList.Add(i); }
            int[] intArray = intList.ToArray();

            using (StreamWriter file = new StreamWriter("MethodIntArray.txt", false)) {
                for (int i = 0; i < 10000; i++) {
                    watch.Start();
                    performance.MethodIntArray(intArray);
                    watch.Stop();
                    string output = string.Format("{0}\t{1}\t{2}", i, "MethodIntArray", watch.Elapsed.ToString());
                    file.WriteLine(output);
                    Console.WriteLine(output);
                    watch.Reset();
                }
            }

            using (StreamWriter file = new StreamWriter("MethodShort.txt", false)) {
                for (int i = 0; i < 10000; i++) {
                    watch.Start();
                    performance.MethodShort(short.MaxValue);
                    watch.Stop();
                    string output = string.Format("{0}\t{1}\t{2}", i, "MethodShort", watch.Elapsed.ToString());
                    file.WriteLine(output);
                    Console.WriteLine(output);
                    watch.Reset();
                }
            }
        }
    }
}
